export const useDictStore = defineStore('dict', () => {
  let dict = $ref([])
  let dictTree = $ref([])
  const treeData = $computed(() => {
    return treeDataList(dictTree, { title: 'category_desc' })
  })
  const getDictTree = async () => {
    await getDictCategorys()
    return treeData
  }
  // 获取字典树
  const getDictCategorys = async (params = {}) => {
    const { status, data } = await Fetch('/api/s/t/dict_category', {
      headers: { timestamp: Date.now() },
      data: objectNilClean({ limit: 5000, params }),
    })
    if (status !== 200 || data.code !== 0) {
      console.log(data.msg)
      return
    }
    if (isNotNilOrEmpty(params)) {
      dictTree = data.result.dataList
    } else {
      return data.result.dataList
    }
  }
  // 根据字典分类获取字典列表
  const getDictByCategoryCode = async (code, update = '') => {
    const idx = dict.findIndex(e => e.category_code === code)
    if (idx < 0 || update === 'update') {
      await getDict({ category_code: code, limit: 500 })
    }
    return dict.filter(e => e.category_code === code)
  }
  // 根据字典分类获取字典的 select 下拉组件列表
  const getDictOptionsByCategoryCode = async (code) => {
    const list = await getDictByCategoryCode(code)
    return getSelectOptions(list, { value: 'dict_code', label: 'dict_desc' })
  }
  // 根据 category_id 获取缓存字典值。
  const getDictByCategoryIdSync = computed(() => (category_id) => {
    return dict.filter(e => e.category_id === category_id)
  })
  // 根据 category_id 获取字典值(如不存在则从线上获取)
  const getDictByCategoryId = async (category_id) => {
    const idx = dict.findIndex(e => e.category_id === category_id)
    if (idx < 0) {
      await getDict({ category_id })
    }
    return dict.filter(e => e.category_id === category_id)
  }
  const cleanDict = (payload) => {
    const { category_code, category_id } = payload
    if (isNotNilOrEmpty(category_id)) {
      dict = dict.filter(e => e.category_id !== category_id)
    } else if (isNotNilOrEmpty(category_code)) {
      dict = dict.filter(e => e.category_code !== category_code)
    }
  }
  // 获取字典表数据
  const getDict = async (payload) => {
    const { status, data } = await Fetch('/api/s/t/dict_data', { headers: { timestamp: Date.now() }, data: { ...payload, is_deleted: 0 } })
    if (status !== 200 || data.code !== 0) { return }
    cleanDict(payload)
    data.result.dataList.forEach(e => {
      const idx = dict.findIndex(v => v.id === e.id)
      if (idx < 0) {
        dict.push(e)
      }
    })
  }
  // 根据字典分类id获取字典列表
  const getDictsByCategoryCode = async (category_code) => {
    const { status, data } = await Fetch('/api/s/t/dict_category', {
      method: 'POST',
      headers: { timestamp: Date.now() },
      data: {
        category_code,
        limit: 500,
        '@recursive': { tableName: "dict_category", fieldName: "category_code" },
      },
    })
    if (status !== 200 || data.code !== 0) {
      console.log(data.msg)
      return []
    }
    return data.result.dataList.filter(e => e.category_code !== category_code)
  }

  return {
    getDict,
    getDictByCategoryId,
    getDictByCategoryIdSync,
    getDictByCategoryCode,
    getDictOptionsByCategoryCode,
    cleanDict,
    getDictsByCategoryCode,
    dictTree: $$(dictTree),
    dict: $$(dict),
    getDictTree,
  }
}, { unistorage: { key: 'config-dict' } })

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useDictStore, import.meta.hot))
